from flask import Blueprint,request,jsonify
from utils.db import db
from utils.comm import get_data
import json
import datetime

bp_project = Blueprint('project',__name__)


#合作商
@bp_project.route('/coop/',methods=["POST","GET"])
def coop():
    if request.method=="POST":
        data = get_data()
        sql = "insert into coopuser(name,code,price) values('%s','%s',%.2f)"%(data['name'],data['code'],float(data['price']))
        db.insert(sql)
        return jsonify({"code":200})
    else:
        sql = "select * from coopuser "
        res = db.find_all(sql)
        return jsonify({"code":200,'list':res})

#跳转到合作页面计算金额
@bp_project.route('/coopprice/',methods=["POST","GET"])
def coopprice():
    if request.method=="POST":
        pass
    else:
        # url = request.remote_addr
        # code = request.args.get('code')
        # price = request.args.get('price')
        # # print(code)
        # stime = datetime.datetime.strftime(datetime.datetime.now(),"%Y-%m-%d %H:%m:%S")
        # # print(stime)
        # sql = "insert into cooprecord values(0,'%s','%s',%.2f)"%(code,stime,float(price))
        # # print(sql)
        # db.insert(sql)

        #链表查询
        
        sql1="select * from (select code,count(code) from cooprecord group by code) as a left join coopuser as c on a.code=c.code"
        res1 = db.find_all(sql1)    
        # print(res1)
        return jsonify({'code':200,'list':res1})

